local AddOnName, Env = ... local ADDON = Env[1]
ADDON.Tools = {}
local Tools = ADDON.Tools

-- ~~| Libraries |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local DiesalTools 	= LibStub("DiesalTools-1.0")
-- ~~| Lua Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local print, type, select, tostring, tonumber		= print, type, select, tostring, tonumber
local concat													= table.concat 
local find, format, lower									= string.find, string.format, string.lower
local tsort														= table.sort
local abs														= math.abs
-- ~~| WoW Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


-- ~~| Development Colors |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

local BLUE			= DiesalTools:GetTxtColor('00aaff')
local DARKBLUE		= DiesalTools:GetTxtColor('004466')
local ORANGE		= DiesalTools:GetTxtColor('ffaa00')
local DARKORANGE	= DiesalTools:GetTxtColor('4c3300')
local GREY			= DiesalTools:GetTxtColor('7f7f7f')
local DARKGREY		= DiesalTools:GetTxtColor('414141')
local WHITE			= DiesalTools:GetTxtColor('ffffff')
local RED			= DiesalTools:GetTxtColor('ff0000')
local GREEN			= DiesalTools:GetTxtColor('00ff2b')
local YELLOW		= DiesalTools:GetTxtColor('ffff00')
local LIGHTYELLOW	= DiesalTools:GetTxtColor('ffea7f')
-- ~~| Development Locals |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local formattedArgs = {}

local function GetCaller(level)
	-- ADDON:LogMessage(debugstack(10,2, 0))	
	for trace in debugstack(level,2, 0):gmatch("(.-)\n") do
		-- Blizzard Sandbox
		local match, _, file, line = trace:find("^.*\\(.-):(%d+)")
		if match then return format('%s[%s%s: %s%s%s]|r',ORANGE,YELLOW,file,LIGHTYELLOW,line,ORANGE) end
		-- PQI DataFile
		local match, _, file,line = trace:find('^%[string "[%s%-]*(.-%.lua).-"%]:(%d+)')
		if match then return format('%s[%s%s: %s%s%s]|r',ORANGE,YELLOW,file,LIGHTYELLOW,line,ORANGE) end
		-- PQR Ability code
		local match, _, file,line = trace:find('^%[string "(.-)"%]:(%d+)')
		if match then return format('%s[%s%s: %s%s%s]|r',ORANGE,YELLOW,file,LIGHTYELLOW,line,ORANGE) end
	end
	return format('%s[%sUnknown Caller%s]|r',ORANGE,RED,ORANGE)
end
local function Print(level,...)	
	local n 			= select('#', ...);
	local caller	= GetCaller(level)
	-- Simple version for single argument
	if (n == 1) then ADDON.Log:AddMessage( format("%s |r%s",caller,tostring(...)) ) return end
	-- Wipe Table
	for i=1, #formattedArgs do	formattedArgs[i] = nil end
	
	local evenArgument
	
	for i = 1, n do
		formattedArgs[i] = format("%s%s",evenArgument and WHITE or BLUE,tostring(select(i, ...)))
		evenArgument = not evenArgument				
	end		
	
	ADDON.Log:AddMessage( format("%s %s",caller,concat(formattedArgs,' ')) )	
end
-- ~~| Development Tools |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


local function printHandler(...) Print(9,...) end
setprinthandler(printHandler)

function Tools.Stack()
	Print(4,"|r------------------------------| Stack Trace |-------------------------------")
	local stack = debugstack(1,12, 0)
	for trace in stack:gmatch("(.-)\n") do		
		match, _, file, line, func = trace:find("^.*\\(.-):(%d+).-`(.*)'$")
		if match then Print(4,format("%s[%s%s: %s%s%s] %sfunction|r %s|r",ORANGE,YELLOW,file,LIGHTYELLOW,line,ORANGE,BLUE,func)) end		
	end
	Print(4,"|r--------------------------------------------------------------------------------")
end
function Tools.Print(...)
		
	Print(4,...)	
end
function Tools.RegionInfo(region)		
	if not region or not region.GetObjectType then return end

	Print(4,"-------------------------------| Region Info |------------------------------")
	Print(4,format('%sName: |r%s',BLUE,region:GetName() or tostring(region) ))
	Print(4,format('%sType: |r%s',BLUE,region:GetObjectType() ))
	Print(4,format('%sWidth: %s%s',BLUE,LIGHTYELLOW,region:GetWidth() ))
	Print(4,format('%sHeight: %s%s',BLUE,LIGHTYELLOW,region:GetHeight() ))
	if region.GetFrameStrata then 					Print(4,format('%sFrameStrata: %s%q',BLUE,GREEN,region:GetFrameStrata() )) end	
	if region.GetFrameLevel then 						Print(4,format('%sFrameLevel: %s%s',BLUE,LIGHTYELLOW,region:GetFrameLevel() )) end	
	if region.GetDrawLayer then						Print(4,format('%sDrawLayer: |r%s',BLUE,region:GetDrawLayer() ))
		if select(2,region:GetDrawLayer()) then 	Print(4,format('%sDrawlevel: %s%s',BLUE,LIGHTYELLOW,select(2,region:GetDrawLayer()) )) end	
	end			
	
	if region:GetNumPoints() then
		for i=1,region:GetNumPoints() do
			local point, relativeTo, relativePoint, xOffset, yOffset = region:GetPoint(i)
			relativeTo = relativeTo and relativeTo:GetName() or relativeTo and tostring(relativeTo) or region:GetParent() and region:GetParent():GetName() or tostring(region:GetParent())
			Print(4,format('%sPoint %s: %s%q|r, %s%s|r, %s%q|r, %s%s|r, %s%s',BLUE,i,GREEN,point,YELLOW,relativeTo,GREEN,relativePoint,LIGHTYELLOW,xOffset,LIGHTYELLOW,yOffset ))
		end
	end
	Print(4,"|r--------------------------------------------------------------------------------")
end
function Tools.PrintRegions(object)
	Print(4,"|r----------------------------| Object Regions |-----------------------------")	
	Print(4,format('%s%s: %s%s',ORANGE,object:GetObjectType(),DARKORANGE,object:GetName() or tostring(object)))
	
	for i=1, object:GetNumRegions() do
		local region = select(i, object:GetRegions())	
		Print(4,format(' |r%s. %s%s: %s%s',i,ORANGE,region:GetObjectType(),DARKORANGE,region:GetName() or tostring(object)))	
		
	end
	Print(4,"|r--------------------------------------------------------------------------------")
end
function Tools.PrintChildren(object)
	Print(4,"|r----------------------------| Object Children |-----------------------------")	
	Print(4,format('%s%s: %s%s',ORANGE,object:GetObjectType(),DARKORANGE,object:GetName() or tostring(object)))
	
	for i=1, object:GetNumChildren() do
		local child = select(i, object:GetChildren())	
		Print(4,format(' |r%s. %s%s: %s%s',i,ORANGE,child:GetObjectType(),DARKORANGE,child:GetName() or tostring(object)))		
	end
	Print(4,"|r--------------------------------------------------------------------------------")
end


function PPNT(...)
		
	Print(4,...)
	
	return {49184,49184,49184}	
end	

-- DT.Stack()
-- local eb = CreateFrame('EditBox')
-- eb:Hide()
-- eb:SetAllPoints(UIParent)
-- DT.PrintRegions(eb)
-- DT.PrintChildren(eb)
-- DT.RegionInfo(eb)
	